import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'
import voca from 'voca'
import { useCommonStore } from '@/stores/common'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes
})

router.beforeEach((to) => {
  if (!voca.isBlank(to.meta.auth)) {
    let store = useCommonStore()
    if (!store.isLogin()) {
      const redirect = encodeURIComponent(to.fullPath)
      return { name: 'login', query: { redirect: redirect } }
    }
  }
})

export default router
